﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace 飞机大战
{
    /// <summary>
    /// 枚举方向
    /// </summary>
    public enum Direction { up, down, left, right }
   
    /// <summary>
    /// 所有游戏对象的父类，封装所有子类所共有的成员
    /// </summary>
    abstract public class Game
    {
        #region 横纵坐标、宽度、高度、速度、生命值、方向
        public int X { get; set; }

        public int Y { get; set; }

        public int Width { get; set; }

        public int Height { get; set; }

        public int Speed { get; set; }

        public int Life { get; set; }

        public Direction dir { get; set; }

        #endregion 
       
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="speed"></param>
        /// <param name="life"></param>
        /// <param name="dir"></param>
        public Game(int x, int y, int width, int height, int speed, int life, Direction dir)
        {
            this.X = x;
            this.Y = y;
            this.Width = width;
            this.Height = height;
            this.Speed = speed;
            this.Life = life;
            this.dir = dir;
        }

        /// <summary>
        /// 绘图函数
        /// </summary>
        /// <param name="g">绘图句柄</param>
        public abstract void Draw(Graphics g);

        /// <summary>
        /// 返回当前游戏对象的矩形，用于碰撞检测
        /// </summary>
        /// <returns></returns>
        public Rectangle GetRectangle()
        {
            return new Rectangle(this.X, this.Y, this.Width, this.Height);
        }

        public Game(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }

        /// <summary>
        /// 移动函数
        /// </summary>
        public virtual void Move()
        {
            //根据游戏对象的方向进行移动
            switch (this.dir)
            {
                case Direction.up:
                    this.Y -= this.Speed;
                    break;
                case Direction.down:
                    this.Y += this.Speed;
                    break;
                case Direction.left:
                    this.X -= this.Speed;
                    break;
                case Direction.right:
                    this.X += this.Speed;
                    break;
            }

            //判断游戏对象是否超出窗体
            if (this.X <= 0)
            {
                this.X = 0;
            }
            if (this.X >= 380)
            {
                this.X = 380;
            }
            if (this.Y <= 0)
            {
                this.Y = 0;
            }
            if (this.Y >= 725)
            {
                this.Y = 725;
            }
        }
    }
}
